﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VNAManagebank.Common;
using VNAManagebank.Bizlogic;
using VNAManagebank.Entity;

namespace VNAManagebank
{
    public partial class frmKhachHang : Form
    {
        public frmKhachHang()
        {
            InitializeComponent();
            HienThi();
        }

        private void HienThi()
        {
            dataGridView1.DataSource = csdl.hienthi("select * from KhachHang");
            FiXDatagridview();
        }

        void FiXDatagridview()
        {
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dataGridView1.ReadOnly = true;

            for (int j = 0; j < dataGridView1.ColumnCount; j++)
            {
                dataGridView1.Columns[j].Visible = false;
            }

            dataGridView1.Columns["MaKhachHang"].Visible = true;
            dataGridView1.Columns["MaKhachHang"].HeaderText = "Mã Khách hàng";
            dataGridView1.Columns["TenKhachHang"].Visible = true;
            dataGridView1.Columns["TenKhachHang"].HeaderText = "Tên Khách hàng";
            dataGridView1.Columns["SCMND"].Visible = true;
            dataGridView1.Columns["SCMND"].HeaderText = "Số CMND";
            dataGridView1.Columns["NoiCap"].Visible = true;
            dataGridView1.Columns["NoiCap"].HeaderText = "Nơi cấp";
            dataGridView1.Columns["NgayCap"].Visible = true;
            dataGridView1.Columns["NgayCap"].HeaderText = "Ngày cấp";
            dataGridView1.Columns["SoDienThoai"].Visible = true;
            dataGridView1.Columns["SoDienThoai"].HeaderText = "Số Điện Thoại";

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.AllowUserToResizeRows = false;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                DataGridViewRow dgvRow = dataGridView1.Rows[e.RowIndex];
                txtMaKhachHang.Text = dgvRow.Cells["MaKhachHang"].Value.ToString();
                txtTenKhachHang.Text = dgvRow.Cells["TenKhachHang"].Value.ToString();
                txtSoCMTND.Text = dgvRow.Cells["SCMND"].Value.ToString();
                txtNoiCap.Text = dgvRow.Cells["NoiCap"].Value.ToString();
                txtNgayCap.Text = dgvRow.Cells["NgayCap"].Value.ToString();
                txtDienThoai.Text = dgvRow.Cells["SoDienThoai"].Value.ToString();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                //Check validate
                if (string.IsNullOrEmpty(txtMaKhachHang.Text))
                {
                    MessageBox.Show("Bạn chưa nhập mã khách hàng", MSG.TieuDe, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtMaKhachHang.Focus();
                    return;
                }

                int kq = csdl.themsuaxoa("insert into KhachHang(MaKhachHang,TenKhachHang,SCMND,NoiCap,NgayCap, SoDienThoai) values(N'" + txtMaKhachHang.Text + "',N'" + txtTenKhachHang.Text + "',N'" + txtSoCMTND.Text + "',N'" + txtNoiCap.Text + "',N'" + txtNgayCap.Text + "',N'" + txtDienThoai.Text + "')");
                HienThi();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                //Check validate
                if (string.IsNullOrEmpty(txtMaKhachHang.Text))
                {
                    MessageBox.Show("Bạn chưa nhập mã khách hàng", MSG.TieuDe, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtMaKhachHang.Focus();
                    return;
                }

                int kq = csdl.themsuaxoa("delete from KhachHang where MaKhachHang = '" + txtMaKhachHang.Text + "'");
                HienThi();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                //Check validate
                if (string.IsNullOrEmpty(txtMaKhachHang.Text))
                {
                    MessageBox.Show("Bạn chưa nhập mã khách hàng", MSG.TieuDe, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtMaKhachHang.Focus();
                    return;
                }

                int kq = csdl.themsuaxoa("update KhachHang set TenKhachHang = N'" + txtTenKhachHang.Text + "', SCMND = N'" + txtSoCMTND.Text + "', NoiCap = N'" + txtNoiCap.Text + "', NgayCap = N'" + txtNgayCap.Text + "', SoDienThoai = N'" + txtDienThoai.Text + "' Where  MaKhachHang = '" + txtMaKhachHang.Text + "'");
                HienThi();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }
    }
}
